<?php
class v1_member{
	function index(){
		$data['basic']=$this->yum->config['Manage']['domain'].'?c=member&a=index';
		$params=array();$params['where']['A.locked !=']=10;
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if(in_array($data['order'],array('money','date'))&&in_array($data['rule'],array('desc','asc'))){
				$params['order'][$data['order']]=$data['rule'];
			}
		}else{
			$data['order']='date';$data['rule']='desc';
			$params['order']=array('date'=>'desc');
		}
		if($this->yum->get('date',true)){
			$data['date']=explode(',',$this->yum->get('date',true));
			if(count($data['date'])==2){
				$params['where']=array('C.date >='=>strtotime($data['date'][0]),'C.date <='=>strtotime($data['date'][1]));
				$data['basic'].='&date='.$data['date'][0].','.$data['date'][1];
			}
		}
		if($this->yum->get('locked',true)){
			$temp=$this->yum->get('locked',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['locked'][$value]=intval($value);
					$data['basic'].='&locked[]='.$value;
				}
				$params['where']['A.locked IN']=$data['locked'];
			}
		}
		if($this->yum->get('role',true)){
			$temp=$this->yum->get('role',true);
			if(is_array($temp)){
				foreach($temp as $key=>$value){
					$data['role'][$value]=intval($value);
					$data['basic'].='&role[]='.$value;
				}
				if(isset($data['role'][0])){
					$temp=array();
					if(!isset($data['role'][1])){$temp[]=1;}
					if(!isset($data['role'][2])){$temp[]=2;}
					if(!empty($temp)){$params['where']['A.role NOT IN']=$temp;}
				}else{
					$params['where']['A.role IN']=$data['role'];
				}
			}
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='mail'){
					$params['where']['A.mail LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='name'){
					$params['where']['C.name LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='phone'){
					$params['where']['C.phone LIKE']='%'.$data['search'].'%';
				}else if($data['tab']=='card'){
					$params['where']['C.IDcard LIKE']='%'.$data['search'].'%';
				}
				$data['basic'].='&tab='.$data['tab'];
				$data['basic'].='&search='.urlencode($data['search']);
			}
		}
		$member=$this->yum->model('member');
		$data['total']=$member->GMemberFull(array('select'=>'COUNT(*) AS count','where'=>$params['where']));
		$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$params['limit']=array(intval($this->yum->get('start')),15);
		$data['member']=$member->GMemberFull($params);
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
	}
	function log(){
		$data['member']=intval($this->yum->get('member'));
    	$data['where']=array('A.member ='=>$data['member']);
    	$data['order']=array('date'=>'desc');
    	$data['limit']=array(intval($this->yum->get('start')),6);
    	$log=$this->yum->model('log');
    	$count=$log->Count(array('where'=>array('member='=>$data['member'])));
    	$count=empty($count)?0:$count[0]['count'];
		$data['sort']=$log->GlogFull($data);
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$this->yum->config['Manage']['domain'].'index.php?c=member&a=log&member='.$data['member'],'total_rows'=>$count,'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('log.php');
	}
	function append(){
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$mail=$this->yum->post('mail',true);$password=$this->yum->post('password',true);$affirm=$this->yum->post('affirm',true);
			$name=$this->yum->post('name',true);$IDcard=$this->yum->post('IDcard',true);$phone=$this->yum->post('phone',true);
			$member=$this->yum->model('member');
			if(preg_match('/^([a-zA-Z0-9_\-\.\+]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/',$mail)==0){
				$output['msg']='账户名必须为邮箱.';
			}else if(preg_match('/[^a-zA-Z_0-9\s]/',$password)!=0){
				$output['msg']='不能为空且只能为a-z;A-Z;0-9;_的组合.';
			}else if(strlen($password)<6){
				$output['msg']='密码长度在6-32之间.';
			}else if($password!=$affirm){
				$output['msg']='两次输入密码不匹配.';
			}else if(count($member->GMember(array('where'=>array('mail ='=>$mail))))>0){
				$output['msg']='邮箱已被注册.';
			}else if(empty($name)){
				$output['msg']='姓名不可为空.';
			}else{
				$mysql=$this->yum->library('mysql');
				$information=$this->yum->model('information');
				$mysql->startTrans();
				$affected=$member->IMember(array('mail'=>$mail,'password'=>$password,'role'=>2,'key'=>md5(uniqid('',true)),'locked'=>0),1);
				$mation=$information->IMation(array('member'=>$affected,'name'=>$name,'IDcard'=>$IDcard,'phone'=>$phone,'date'=>$this->yum->server('REQUEST_TIME'),'ip'=>ip2long($this->yum->server('REMOTE_ADDR'))));
				if(($mation==0)||($affected==0)){
					$mysql->rollback();
					$output['msg']='表单数据有误.';
				}else{
					$mysql->commit();
					$output['result']=2;
					$output['msg']='添加成功.';
				}
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$this->yum->view('append.php');
		}
	}
	function edit(){
		$member=$this->yum->model('member');
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$id=$this->yum->post('member',true);$role=$this->yum->post('role',true);$name=$this->yum->post('name',true);
			$IDcard=$this->yum->post('IDcard',true);$phone=$this->yum->post('phone',true);$msn=$this->yum->post('msn',true);
			$qq=$this->yum->post('qq',true);
			$affected_1=$member->UMember(array('where'=>array('id='=>$id),'set'=>array('role='=>$role)));
			$information=$this->yum->model('information');
			$affected_2=$information->UMation(array('where'=>array('member='=>$id),'set'=>array('name='=>$name,'IDcard='=>$IDcard,'phone='=>$phone,'msn='=>$msn,'qq='=>$qq)));
			if($affected_1==1||$affected_2==1){
				$output['result']=2;
				$output['msg']='修改成功.';
			}else{
				$output['msg']='请误提交相同或未经修改的数据.';
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$role=$this->yum->model('role');
			$data['role']=$role->GRole(array('where'=>array('locked ='=>0)));
			$params['where']=array('A.id ='=>intval($this->yum->get('member',true)));
			$data['member']=$member->GMemberFull($params);
			$data['member']=reset($data['member']);
			$this->yum->set($data);
			$this->yum->view('edit.php');
		}
	}
	function lock(){
		$output=array('result'=>0,'msg'=>'');
		$member=$this->yum->model('member');
		$id=$this->yum->get('member');
		$locked=intval($this->yum->get('locked',true));
		if(is_array($id)){
			$id=array_map('intval',$id);
			if(array_search(1,$id)!==false){
				$output['msg']='超级管理员不可修改.';
			}else{
				$affected=$member->UMember(array('where'=>array('id IN'=>$id),'set'=>array('locked='=>$locked)));
				if($affected==0){
					$output['msg']='重复的数据提交.';
				}else{
					$output['result']=2;
				}
			}
		}else{
			if(intval($id)==1){
				$output['msg']='超级管理员不可删除.';
			}else{
				$affected=$member->UMember(array('where'=>array('id='=>$id),'set'=>array('locked='=>$locked)));
				if($affected==1){
					$output['result']=5;
					$output['td']=array('offset'=>9,'data'=>array($locked==0?'正常':'禁止'));
					$output['exec']=array('attr'=>array('mixed'=>'sub:{locked:'.($locked==0?2:0).'}'),'text'=>($locked==0?'禁止':'激活'));
				}else{
					$output['msg']='成员状态已变更.';
				}
			}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
	function wipe(){
		$output=array('result'=>0,'msg'=>'');
		$member=$this->yum->model('member');
		$id=$this->yum->get('member');
		if(is_array($id)){
			$id=array_map('intval',$id);
			if(array_search(1,$id)!==false){
				$output['msg']='超级管理员不可删除.';
			}else{
				$affected=$member->UMember(array('where'=>array('id IN'=>$id),'set'=>array('locked='=>10)));
				if($affected==0){
					$output['msg']='重复的数据提交.';
				}else{
					$output['result']=2;
				}
			}
		}else{
			if(intval($id)==1){
				$output['msg']='超级管理员不可删除.';
			}else{
				$affected=$member->UMember(array('where'=>array('id='=>$id),'set'=>array('locked='=>10)));
				if($affected==1){$output['result']=1;}else{$output['msg']='已删除.';}
			}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
}
?>